@extends('admin.default')
@section('title', '科目管理')
@section('content')
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            @if($errors->any())
                                @foreach($errors->all as $error)
                                    {{$error}}
                                @endforeach

                                @endif
                            <form class="layui-form layui-col-space5" id="add_from">
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" placeholder="科目名" id='name' name="name">
                                    <input class="layui-input" type="hidden" name="parent_id" value="0">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="add"><i class="layui-icon"></i>增加顶级科目</button>
                                </div>
                                {{csrf_field()}}
                            </form>


                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <button type="button" class="layui-btn" id="btnFoldAll"><i class="layui-icon">&#xe619;</i>全部折叠</button>
                            <button type="button" class="layui-btn" id="btnExpandAll"><i class="layui-icon">&#xe61a;</i>全部展开</button>

                        </div>
                        <div class="layui-card-body ">
                            <table id="subject-tree" lay-filter="subject-tree"></table>
                        </div>
                        <div class="layui-card-body "> </div>
                    </div>
                </div>
            </div>
        </div>
        <script type="text/html" id="tbBar">
            <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑','/admin/subject/@{{d.id}}/edit',600,300)" ><i class="layui-icon">&#xe642;</i>编辑</button>
            <button class="layui-btn layui-btn-warm layui-btn-xs  @{{d.parent_id>0?'layui-btn-disabled':''}}"    @{{d.parent_id>0?'disabled="disabled"':''}} onclick="xadmin.open('添加','/admin/subject/@{{d.id}}',600,300)" ><i class="layui-icon">&#xe642;</i>添加子科目</button>
            <button class="layui-btn-danger layui-btn layui-btn-xs"    onclick="subject_del(this,'/admin/subject/@{{d.id}}')" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
        </script>

        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <script>
            layui.config({
                base: '/static/js/'
            }).use(['form','treeTable','layer','element'], function(){
                var form = layui.form;
                var treeTable = layui.treeTable;
              form.on('submit(add)', function(data){
                  data = data.field;
                  if (data.name.length < 2) {
                      layer.tips('科目名必须大于1个字符', '#name', {
                          tips: [3, '#009688']
                      });
                      return false;
                  }
                  $.ajax({
                      type: 'POST',
                      url: '{{route('admin.subject.store')}}',
                      data:data,
                      dataType: 'json',
                      success: function (data) {
                          if(data == 1){
                              layer.alert('添加成功', {
                                  icon: 1,
                                  skin: 'layui-layer-molv' //样式类名
                                  ,closeBtn: 0
                              }, function(){
                                  window.location.reload();
                                  $("#add_from")[0].reset();
                              });
                          } else{
                              layer.alert('添加失敗', {
                                  icon: 2,
                                  skin: 'layui-layer-molv' //样式类名
                                  ,closeBtn: 0
                              });
                          }
                      },error: function(res) {
                          var msg=JSON.parse(res.responseText);
                          layer.alert(msg.errors['name'][0], {
                              icon: 2,
                              title: '提示信息',
                              skin: 'layui-layer-molv' //样式类名
                              ,closeBtn: 0
                          });
                      }
                  });
                  return false;
              });//表单监听完成
                var insTb = treeTable.render({
                    elem: '#subject-tree',
                    url: '{{route('api.subjects.list')}}',
                    text: {
                        none: '<div style="padding: 18px 0;">无科目信息~</div>'
                    },
                    tree: {
                        iconIndex: 1,
                        isPidData: true,
                        treeSpid: 0,
                        idName: 'id',  // id字段名称
                        pidName: 'parent_id',
                    },
                    cols: [
                        [
                            {title: '注意：只有顶级科目可以添加子科目。如 顶级科目“一级建造师” 下可以添加 “工程法规” 子科目', colspan: 5},
                        ],
                        [
                        {field: 'id',width: 80,title: '科目ID'},
                        /*{type: 'checkbox'},*/
                        {field: 'name', width: 540, title: '科目名称'},
                        {align: 'center',width: 200, title: '题库',templet:function (d) {
                            if(d.parent_id>0){
                                return '<a href="javascript:xadmin.add_tab_f(\'题库-'+ d.name+'\',\'/admin/question/subjectid-'+d.id+'/show\',true);"><span class="layui-badge layui-badge-green">题库</span></a>';
                            } else {
                                return 'N/A';
                            }
                        }},
                        {align: 'center',width: 200, title: '章节',templet:function (d) {
                                if(d.parent_id>0) {
                                    return '<a href="javascript:xadmin.add_tab_f(\'章节-' + d.name + '\',\'/admin/sections/subjectid-' + d.id + '/index\');"><span class="layui-badge layui-bg-orange">章节</span></a>';
                                }else{
                                    return 'N/A';
                                }
                                }},
                        {align: 'center', title: '操作',templet:'#tbBar'}
                    ]],

                });
                $('#btnExpandAll').click(function () {
                    insTb.expandAll();
                });
                $('#btnFoldAll').click(function () {
                    insTb.foldAll();
                });

          });

           /*用户-删除*/
          function subject_del(obj,url){
              layer.confirm('确认要删除吗？',function(index){
                  $.ajax({
                      type: 'post',
                      url: url,
                      data:{
                          '_token':'{{csrf_token()}}',
                          '_method':'DELETE'
                      },
                      dataType: 'json',
                      success: function (data) {
                          if(data == 1){
                              layer.alert('删除成功', {
                                  icon: 1,
                                  skin: 'layui-layer-molv' //样式类名
                                  ,closeBtn: 0
                              }, function(){
                                  window.location.reload();
                              });
                          } else{
                              layer.alert('删除失敗', {
                                  icon: 2,
                                  skin: 'layui-layer-molv' //样式类名
                                  ,closeBtn: 0
                              });
                          }
                      },error: function(res) {
                          var msg=JSON.parse(res.responseText);
                          layer.alert('删除失敗', {
                              icon: 2,
                              title: '提示信息',
                              skin: 'layui-layer-molv' //样式类名
                              ,closeBtn: 0
                          });
                      }
                  });

              });
          }








        </script>
@stop
